﻿using BestTrader.Cms.Core.Options;
using BestTrader.Cms.GomePay.Models;
using BestTrader.Cms.ViewModels;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BestTrader.Cms.GomePayCelientServices
{
    public class GomePayDFCelientService
    {

        protected GomePayOption _gomePayOption;
        protected ILogger<GomePayDFCelientService> _logger;
        public HttpClient Client { get; private set; }

        public GomePayDFCelientService(IOptionsSnapshot<GomePayOption> options, HttpClient httpClient, ILogger<GomePayDFCelientService> logger)
        {
            _gomePayOption = options.Get("GOMEPAY");
            httpClient.BaseAddress = new Uri(_gomePayOption.BaseUrl);
            //httpClient.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            //httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
            Client = httpClient;
            _logger = logger;
        }

      
        public async Task<string> AddGomePayDF(GomePayDfModel input)
        {
            var url = _gomePayOption.BaseUrl;
            //var data = orderNo; // DateTime.Now.ToString("yyyyMMddHHmmss");
            //var money = amount.ToString(); //"1.00";
            //var token = GetYunPayToken(money, ip4, data, type);

            List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();

            //order_number = TEST100000000000001 & customer_name = zzc & amount = 0.01 & account_number = 6217850800012786339 & issue_bank_name = 中国银行 & memo = 11111

            paramList.Add(new KeyValuePair<string, string>("currency", input.Currency));
            paramList.Add(new KeyValuePair<string, string>("amount", input.Amount));
            paramList.Add(new KeyValuePair<string, string>("order_number", input.OrderNumber));
            paramList.Add(new KeyValuePair<string, string>("customer_name",input.CustomerNumber));
            paramList.Add(new KeyValuePair<string, string>("account_number", input.AccountNumber));
            paramList.Add(new KeyValuePair<string, string>("issue_bank_name", input.IssueBankName));
            paramList.Add(new KeyValuePair<string, string>("memo", input.Memo)); 

            var response = await Client.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList));

            return await response.Content.ReadAsStringAsync();

        }

        //public async Task<string> GetYunPayOrder(string orderNo, string ip4, string type = "1")
        //{
        //    var url = $"{_gomePayOption.BaseUrl}order";
        //    //var token = GetOrderToken(ip4, type);

        //    List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
        //    paramList.Add(new KeyValuePair<string, string>("appid", _payOption.AppId));
        //    paramList.Add(new KeyValuePair<string, string>("order", orderNo));
        //    paramList.Add(new KeyValuePair<string, string>("type", type));
        //    paramList.Add(new KeyValuePair<string, string>("uip", ip4));
        //    paramList.Add(new KeyValuePair<string, string>("token", token));
        //    var response = await Client.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList));
        //    return await response.Content.ReadAsStringAsync();
        //}






    }
}
